<template>
    <div class="global">
        <div class="write">
            <div class="item">
                <el-select v-model="typeId" placeholder="请选择分类" style="width: 200px;">
                    <el-option v-for="item, index in list" :key="item.id" :label="item.typename"
                        :value="item.id"></el-option>
                </el-select>
            </div>
            <div class="item">
                <el-input placeholder="请输入标题" v-model="title" style="width: 200px;"></el-input>
            </div>

            <div id="editor"></div>
            <div style="margin-top: 20px;">
                <el-button type="primary" @click="submit()">提交</el-button>
            </div>
        </div>
    </div>
</template>
<script>
import service from '@/request';
import E from 'wangeditor';
export default {
    data() {
        return {
            list: [],
            content: "",
            editor: null,
            user: null,
            title: "",
              
                typeId:"",
                content:""
        }
    },
    mounted() {
        this.user = JSON.parse(window.localStorage.getItem("user"))
        // this.content=this.editor.txt.html();
        this.initWangEditor(''); //每次点击新增按钮，都清空编辑器文本内容
        this.gettype()
    },
    methods: {
        async submit() {

            this.content=this.editor.txt.html()
            console.log(this.editor.txt.html())
            console.log(this.content)
            if(this.title == ''){
                this.$message.warning("请输入标题")
                return
            }         
            if(this.type == ''){
                this.$message.warning("请选择分类")
                return
            }         
            if(this.content == ''){
                this.$message.warning("请输入内容")
                return
            }            
            const res = await service.post("/article/add", {
                title: this.title,
                userId: this.user.id,
                typeId:this.typeId,
                content: this.content

            })
            console.log(res)
            if(res.code == '0'){
                this.$router.back()
            }
        },
        // 初始化编辑器
        initWangEditor(content) { //用户输入的文本作为参数传进函数。
            this.$nextTick(() => { //1.dom元素加载好后，先判断editor是否已经完成初始化，如果已经完成初始化，那么把editor销毁，清空。
                if (this.editor) {
                    this.editor.destroy();
                    this.editor = null;
                }
                this.editor = new E('#editor') //2.新建一个editor，赋值给我们一开始定义的editor，并渲染在iD为#editor的元素里
                this.editor.config.placeholder = '请输入内容' // 配置编辑器默认展示的文本
                this.editor.config.uploadFileName = 'file' //配置编辑器文件上传的名称
                this.editor.config.uploadImgServer = 'http://101.35.211.241:8099/files/wang/upload'  //配置编辑器文件上传的接口
                this.editor.create() //执行创建编辑器
                setTimeout(() => { //延时函数，这里没有设置延时时间，即为实时获取编辑器的文本给到this.editor.txt.html()。
                    this.editor.txt.html(content)
                })

            })
        },
        async gettype() {
            const res = await service.get(`/type/list`);
            console.log(res)
            this.list = res.data

        },
    }
}

</script>
<style scoped>
.item {
    margin-bottom: 30px;
}

.write {
    width: 100%;

    background: #ffff;
    padding: 10px;
    box-sizing: border-box;

}

.global {
    width: 100%;
    height: 100%;
    padding: 10px;
    box-sizing: border-box;
}
</style>